package zifuchuan;

public class _08按单词翻转字符串 {
    //将字符串按单词翻转，例如 here you are 翻转成are you here
    public static void main(String[] args) {
        String initString = "here you are";
        String reverse = reverse(initString);
        System.out.println(reverse);
    }


    //将整个字符串按照字符翻转，再找到每个单词，将单词翻转
    public static String reverse(String initString){
        String s1 = reverseString(initString);
        StringBuilder sb = new StringBuilder();
        //分割出每一个单词
        String[] words = s1.split("\\s");
        for (int i = 0 ; i < words.length; i++){
            sb.append(reverseString(words[i])+ " ");
        }
        return sb.deleteCharAt(sb.length()-1).toString();
    }

    public static String reverseString(String iniString) {
        StringBuffer sb = new StringBuffer(iniString);
        return sb.reverse().toString();
    }
}
